#include "Sys_local.h"
#include "../infLib/infLib.h"

TranslateState translate[256];

inf::StaticList<sysEvent_t, 256> eventQueue;
int eventTail = 0;
int eventKeyboardAccum = 0;
int eventMouseAccum = 0;

void Sys_QueEvent( int time, sysEventType_t type, int key, int value, int value2, int ptrLength, void *ptr ) {
    sysEvent_t ev;
    ev.evType = type;
    ev.evKey = key;
    ev.evValue = value;
    ev.evValue2 = value2;
    ev.evPtrLength = ptrLength;
    ev.evPtr = ptr;

    eventQueue.append( ev );

    if( ev.evType == SE_KEY ) {
        eventKeyboardAccum++;
    }else if( ev.evType == SE_MOUSE ) {
        eventMouseAccum++;
    }
}

sysEvent_t		Sys_GetEvent( void ) {
    static sysEvent_t empty;

    if( eventTail >= eventQueue.size() ) {
        inf::memory_set( &empty, 0, sizeof(empty) );

        return empty;
    }

    sysEvent_t* pE = &eventQueue[eventTail];

    ++eventTail;
    return *pE;
}

void			Sys_ClearEvents( void ) {
    eventQueue.clear();
    eventTail = 0;
    eventKeyboardAccum = 0;
    eventMouseAccum = 0;
}

void			Sys_InitInput( void ) {
    Sys_ClearEvents();

    // setup keyboard data
    for ( int i = 0; i < 256 ; ++i ) // setup char keys
    {
        translate[i].keyc = (sysKey_t) i;
        translate[i].down = false;
    }

    translate[219].keyc = K_OpenBracket;  // setup control keys
    translate[221].keyc = K_CloseBracket;
    translate[220].keyc = K_BackSlash;
    translate[13].keyc  = K_Enter;
    translate[187].keyc = K_Equals;
    translate[189].keyc = K_Separator;
    translate[186].keyc = K_SemiColon;
    translate[191].keyc = K_Slash;
    translate[190].keyc = K_Period;
    translate[188].keyc = K_Comma;
    translate[45].keyc  = K_Insert;
    translate[46].keyc  = K_Delete;

}

void			Sys_ShutdownInput( void ) {
    // nothing
}

unsigned char	Sys_MapCharForKey( int key ) {
    return key;
}

int				Sys_PollKeyboardInputEvents( void ) {
    assert( eventKeyboardAccum <= eventQueue.size() );

    // will add lock

    return eventKeyboardAccum;
}

int				Sys_ReturnKeyboardInputEvent( const int n, int &ch, bool &state ) {
    for( int i=n; i<eventQueue.size(); ++i ) {
        if( eventQueue[i].evType == SE_KEY ) {
            ch = Sys_MapCharForKey(eventQueue[i].evKey);
            state = static_cast<bool>(eventQueue[i].evValue);
            return i+1;
        }
    }

    return -1;
}

void			Sys_EndKeyboardInputEvents( void ) {
    // will add unlock
}

int				Sys_PollMouseInputEvents( void ) {
    assert( eventMouseAccum <= eventQueue.size() );

    // will add lock

    return eventMouseAccum;
}

int				Sys_ReturnMouseInputEvent( const int n, int &action, int &value, int& value2 ) {
    for( int i=n; i<eventQueue.size(); ++i ) {
        if( eventQueue[i].evType == SE_MOUSE ) {
            action = eventQueue[i].evKey;
            value = eventQueue[i].evValue;
            value2 = eventQueue[i].evValue2;
            return i+1;
        }
    }

    return -1;
}

void			Sys_EndMouseInputEvents( void ) {
    // will add unlock
}
